import java.rmi.Naming;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.Timer;										//@PEDRO

import java.awt.*;
import java.awt.event.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StartServer {
	ChatServerInt server;										//@PEDRO
	String registry;											//@PEDRO
	
	public void RegisterServer()								//@PEDRO
	{		
		try {
			Naming.rebind("rmi://"+ registry +"/server", server);
			System.out.println("[System] Servidor Registrado");
		} catch (Exception ex) {
			System.err.println("Servidor de chat fallo: "+ ex);
		}
	}
	
	Timer timer = new Timer( 5000, new ActionListener(){  		//@PEDRO
	  @Override
	  public void actionPerformed( ActionEvent e ){
		  try 
		  {
			if (!server.getServerAlive())
				RegisterServer();
		  }
		catch (RemoteException re) {}
	  }
	} );
	
	public static void main(String[] args) {
		String registry="localhost";							//@PEDRO	
	
		if (args.length >=1){
			registry=args[0];
		}

		StartServer s = new StartServer(registry);				//@PEDRO
	}
	
	public StartServer(String reg) 								//@PEDRO
	{
		try {
			registry = reg;
			server = new ChatServer();

			//RegisterServer();		  
			
			//System.out.println("[System] Servidor de chat Registrado.");
			
			timer.setRepeats( true );
			timer.start();
			
		} catch (Exception ex) {
			System.err.println("Servidor de chat fallo: " + ex);
		}
	}
}